package com.zoffy.wmsbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoffy.wmsbackend.common.ErrorCode;
import com.zoffy.wmsbackend.common.RequestParam;
import com.zoffy.wmsbackend.exception.BusinessException;
import com.zoffy.wmsbackend.model.dto.goods.GoodsDTO;
import com.zoffy.wmsbackend.model.entity.Goods;
import com.zoffy.wmsbackend.model.vo.GoodsVO;
import com.zoffy.wmsbackend.service.GoodsService;
import com.zoffy.wmsbackend.mapper.GoodsMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
* @author 86170
* @description 针对表【goods(货品表)】的数据库操作Service实现
* @createDate 2025-01-02 01:34:39
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService {

    @Override
    public Boolean addGoods(GoodsDTO dto) {
        String goodsName = dto.getGoodsName();
        String tally = dto.getTally();
        Integer amountGd = dto.getAmountGd();
        if(StringUtils.isAnyBlank(goodsName,tally)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Goods goods=new Goods();
        goods.setGoodsName(goodsName);
        String goodsCode = Integer.toHexString(goodsName.hashCode()).substring(0,Math.min(goodsName.length(),8))+ UUID.randomUUID().toString().replace("-","").substring(0,8);
        goods.setGoodsCode(goodsCode);
        goods.setTally(tally);
        goods.setAmountGd(amountGd);

        return this.save(goods);
    }

    @Override
    public Boolean deleteGoods(GoodsDTO dto) {
        Long id = dto.getId();
        return this.removeById(id);
    }

    @Override
    public Boolean updateGoods(GoodsDTO dto) {
        Long id = dto.getId();
        Goods goods = this.getById(id);
        String goodsName = dto.getGoodsName();
        String tally = dto.getTally();
        Integer amountGd = dto.getAmountGd();
        if(StringUtils.isNotBlank(goodsName)){
            goods.setGoodsName(goodsName);
        }
        if(StringUtils.isNotBlank(tally)){
            goods.setTally(tally);
        }
        if(ObjectUtils.isNotEmpty(amountGd)){
            goods.setAmountGd(amountGd);
        }
        return this.updateById(goods);
    }

    @Override
    public IPage<GoodsVO> listGoodsByPage(RequestParam<GoodsDTO> requestParam) {
        Integer pageNum = requestParam.getPageNum();
        Long pageSize = requestParam.getPageSize();
        GoodsDTO params = requestParam.getParams();
        String goodsName = null;
        String goodsCode = null;
        if(params!= null){
            goodsName = params.getGoodsName();
            goodsCode = params.getGoodsCode();
        }
        LambdaQueryWrapper<Goods> wrapper= Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(goodsName),Goods::getGoodsName,"%"+goodsName+"%");
        wrapper.eq(StringUtils.isNotBlank(goodsCode),Goods::getGoodsCode,goodsCode);

        Page<Goods> page=new Page<>(pageNum,pageSize);
        Page<Goods> goodsPage = this.page(page, wrapper);
        return  goodsPage.convert(GoodsVO::obj2VO);
    }
}




